<template>
  <div class="v-container" style="background-color:#fff;color:#000">
    <v-header title="机场列表">
      <span class="back" @click="goBack">
        <i class="el-icon-arrow-left"></i>
      </span>
    </v-header>
    <div class="mt">
      <mt-search style="height:100%" v-model="value" cancel-text="取消" placeholder="搜索"></mt-search>
      <div v-if="value !== ''">
        <mt-cell
          :title="item1.airportName"
          v-for="(item1, index1) in list"
          :key="index1"
          @click.native="clickxome(item1)"
          style="margin-left:10px"
        ></mt-cell>
      </div>

      <mt-index-list v-if="value == ''">
        <mt-index-section :index="item.letter" v-for="(item, index) in airportList" :key="index">
          <mt-cell
            :title="item1.airportName"
            v-for="(item1, index1) in item.data"
            :key="index1"
            @click.native="clickxome(item1)"
            style="margin-left:10px"
          ></mt-cell>
        </mt-index-section>
      </mt-index-list>
    </div>
  </div>
</template>

<script>
const proxy = require("@/env/proxy");
const airport = proxy.airport;
const query_page = proxy.query_page;
import { Resource } from "@/services/resource";
import axios from "axios";
import { Toast } from "mint-ui";
import vHeader from "@/components/Header.vue";
import countDown from "@/components/CountDown.vue";

export default {
  data() {
    return {
      airportList: [],
      value: "",
      list: [],
      defaultResult: []
    };
  },
  watch: {
    value() {
      this.list = [];
      if (
        this.defaultResult != undefined &&
        this.defaultResult != null &&
        this.defaultResult.length != 0
      ) {
        for (var i = 0; i < this.defaultResult.length; i++) {
          var num = this.defaultResult[i].airportName.indexOf(this.value);
          if (num != -1) {
            this.list.push(this.defaultResult[i]);
          }
        }
      }
    }
  },
  components: {
    vHeader,
    countDown
  },
  created() {
    let _this = this;
    Resource.get
      .post(
        {},
        {
          url: airport
        }
      )
      .then(res => {
        console.log(JSON.parse(res.body.result).data);

        _this.airportList = [];
        let data = JSON.parse(res.body.result).data;
        data.forEach((item, index) => {
          item.airportCityBeanList.forEach((item1, index1) => {
            item1.airportInfoBeanList.forEach((item2, index2) => {
              _this.airportList.push(item2);
            });
          });
        });
        _this.airportList.forEach((item, index) => {
          _this.defaultResult.push(item);
        });
        console.log(_this.defaultResult);
        console.log(_this.pySort(_this.airportList));
        _this.airportList = _this.pySort(_this.airportList);
      })
      .catch(err => {
        console.log(err);
      });
  },
  methods: {
    // 返回上一页
    goBack() {
      if (this.$route.query.type == null) {
        this.$router.push({
          path: "/airportVipForm",
          query: { bespeakTime: this.$route.query.bespeakTime }
        });
      } else {
        this.$router.push("/airportParkingForm");
      }
    },
    pySort: function(arr, empty) {
      var $this = this;
      if (!String.prototype.localeCompare) return null;
      var letters = "ABCDEFGHJKLMNOPQRSTWXYZ".split("");
      var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split("");
      var arrList = [];
      for (var m = 0; m < arr.length; m++) {
        arrList.push(arr[m]);
      }
      console.log(arrList);
      var result = [];
      var curr;
      for (var i = 0; i < letters.length; i++) {
        curr = { letter: letters[i], data: [] };
        if (i != 26) {
          for (var j = 0; j < arrList.length; j++) {
            var initial = arrList[j].airportName.charAt(0); //截取第一个字符
            if (
              arrList[j].airportName.charAt(0) == letters[i] ||
              arrList[j].airportName.charAt(0) == letters[i].toLowerCase()
            ) {
              //首字符是英文的
              curr.data.push(arrList[j]);
            } else if (zh[i] != "*" && $this.isChinese(initial)) {
              //判断是否是无汉字,是否是中文
              if (
                initial.localeCompare(zh[i]) >= 0 &&
                (!zh[i + 1] || initial.localeCompare(zh[i + 1]) < 0)
              ) {
                //判断中文字符在哪一个类别
                curr.data.push(arrList[j]);
              }
            }
          }
        } else {
          for (var k = 0; k < arrList.length; k++) {
            var ini = arrList[k].airportName.charAt(0); //截取第一个字符
            if (!$this.isChar(ini) && !$this.isChinese(ini)) {
              curr.data.push(arrList[k]);
            }
          }
        }
        if (empty || curr.data.length) {
          result.push(curr);
          //curr.data.sort(function(a,b){
          //  return b.localeCompare(a);    //排序,英文排序,汉字排在英文后面
          //});
        }
      }
      return result;
    },
    isChinese: function(temp) {
      var re = /[^\u4E00-\u9FA5]/;
      if (re.test(temp)) {
        return false;
      }
      return true;
    },
    isChar: function(char) {
      var reg = /[A-Za-z]/;
      if (!reg.test(char)) {
        return false;
      }
      return true;
    },
    clickxome(item1) {
      let airportName = item1.airportName.slice(0, 5);
      if (this.$route.query.type == null) {
        let param = {
          pageNum: "1",
          pageSize: "100",
          secondarySupplierType: "2",
          airportName: airportName
        };
        Resource.jsonPost
          .post(
            {},
            {
              url: query_page,
              jsonparams: param
            }
          )
          .then(res => {
            if (JSON.parse(res.body.result).data.dataList.length > 0) {
              this.$router.push({
                path: "/airportVipForm",
                query: {
                  airportCode: item1.airportCode,
                  airportName: item1.airportName,
                  bespeakTime: this.$route.query.bespeakTime
                }
              });
            } else {
              Toast({
                message: "该机场暂无服务资源",
                position: "middle"
              });
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$router.push({
          path: "/airportParkingForm",
          query: {
            airportCode: item1.airportCode,
            airportName: item1.airportName
          }
        });
      }

      console.log(item1);
    }
  }
};
</script>

<style lang="less" scoped>
@import url("./style.less");
</style>
<style scoped>
li >>> .mint-field-other {
  position: static;
}
</style>